package km

import (
	"database/sql"
	"errors"
	"time"

	"log"

	"github.com/axgle/mahonia"
)

//PmTPriceFlowDetailItem 特价信息
type PmTPriceFlowDetailItem struct {
	FlowID      int
	SheetNo     string
	ItemNo      string
	ItemSubno   string
	ItemName    string
	ItemSubname string
	UnitNo      string
	OldPrice    float64
	NewPrice    float64
	OldInPrice  float64
	NewInPrice  float64
	StartDate   time.Time
	EndDate     time.Time
}

//GetPmTPriceFlowDetailItemsBySheetNo 通过促销计划销售单号获取特价列表
func GetPmTPriceFlowDetailItemsBySheetNo(sheetNo string) ([]*PmTPriceFlowDetailItem, error) {
	var ptfdis []*PmTPriceFlowDetailItem
	if !(len(sheetNo) == 14 && sheetNo[0:2] == "PP") {
		return ptfdis, errors.New("sheetNo invalid")
	}
	conn, err := sql.Open("odbc", KMJXCConnStr)
	if err != nil {
		return ptfdis, err
	}
	defer conn.Close()

	stmt, err := conn.Prepare(`SELECT pm.flow_id,pm.sheet_no,pm.item_no,pm.old_price,pm.new_price,pm.start_date,pm.end_date,bi.item_name,bi.unit_no,bi.item_subno,bi.item_subname AS item_subname,  pm.old_inprice,   pm.new_inprice FROM pm_t_price_flow_detial pm,bi_t_item_info bi WHERE pm.item_no = bi.item_no and pm.sheet_no = ?`)
	if err != nil {
		return ptfdis, err
	}
	defer stmt.Close()
	row, _ := stmt.Query(sheetNo)
	defer row.Close()
	dec := mahonia.NewDecoder("gbk")
	for row.Next() {
		var ptfdi PmTPriceFlowDetailItem
		row.Scan(&ptfdi.FlowID, &ptfdi.SheetNo, &ptfdi.ItemNo, &ptfdi.OldPrice, &ptfdi.NewPrice, &ptfdi.StartDate, &ptfdi.EndDate, &ptfdi.ItemName, &ptfdi.UnitNo, &ptfdi.ItemSubno, &ptfdi.ItemSubname, &ptfdi.OldInPrice, &ptfdi.NewInPrice)
		ptfdi.ItemName = dec.ConvertString(ptfdi.ItemName)
		ptfdi.ItemSubname = dec.ConvertString(ptfdi.ItemSubname)
		ptfdi.UnitNo = dec.ConvertString(ptfdi.UnitNo)
		ptfdis = append(ptfdis, &ptfdi)
	}
	return ptfdis, nil
}

func GetPmTPriceFlowDetailItemsForNews() [3][]*PmTPriceFlowDetailItem {
	var ptfdis [3][]*PmTPriceFlowDetailItem

	conn, err := sql.Open("odbc", KMJXCConnStr)
	if err != nil {
		log.Println(err)
	}
	defer conn.Close()

	stmt, err := conn.Prepare(`SELECT top 5 pm.flow_id,pm.sheet_no,pm.item_no,pm.old_price,pm.new_price,pm.start_date,pm.end_date,bi.item_name,bi.unit_no,bi.item_subno,bi.item_subname AS item_subname,  pm.old_inprice,   pm.new_inprice FROM pm_t_price_flow_detial pm,pm_t_flow_main main,bi_t_item_info bi WHERE pm.item_no = bi.item_no and pm.sheet_no = main.sheet_no and pm.start_date < getdate() and pm.end_date > getdate()  and main.branch_no = ? order by pm.start_date desc`)
	if err != nil {
		log.Println(err)
	}
	defer stmt.Close()
	mapInfos := map[int]string{0: "*", 1: "00", 2: "03"}
	for k, v := range mapInfos {
		row, _ := stmt.Query(v)
		defer row.Close()
		dec := mahonia.NewDecoder("gbk")
		for row.Next() {
			var ptfdi PmTPriceFlowDetailItem
			row.Scan(&ptfdi.FlowID, &ptfdi.SheetNo, &ptfdi.ItemNo, &ptfdi.OldPrice, &ptfdi.NewPrice, &ptfdi.StartDate, &ptfdi.EndDate, &ptfdi.ItemName, &ptfdi.UnitNo, &ptfdi.ItemSubno, &ptfdi.ItemSubname, &ptfdi.OldInPrice, &ptfdi.NewInPrice)
			ptfdi.ItemName = dec.ConvertString(ptfdi.ItemName)
			ptfdi.ItemSubname = dec.ConvertString(ptfdi.ItemSubname)
			ptfdi.UnitNo = dec.ConvertString(ptfdi.UnitNo)
			ptfdis[k] = append(ptfdis[k], &ptfdi)
		}
	}
	return ptfdis
}
